﻿using Audio;
using Combat;
using Controls;
using UnityEngine;

namespace Level
{
    public class GameStartedState : MonoBehaviour, IState
    {
        // [SerializeField] private VoidEventChannel gameStartedEvent;
        
        [SerializeField] private Health player;
        [SerializeField] private PlayerController playerController;

        private GameStateMachine _stateMachine;

        private bool _firstEnter = true;
        
        
        public IState Init(GameStateMachine stateMachine)
        {
            _stateMachine = stateMachine;
            return this;
        }

        public void Enter()
        {
            if (_firstEnter)
            {
                _firstEnter = false;
                AudioPlayer.Instance.SetVolume(1f);
                // gameStartedEvent.Raise();
            }
            
            player.Died += Player_Died;
            playerController.MenuPerformed += PlayerController_MenuPerformed;
        }

        public void Exit()
        {
            player.Died -= Player_Died;
            playerController.MenuPerformed -= PlayerController_MenuPerformed;
        }
        
        private void Player_Died()
        {
            player.Died -= Player_Died;
            _stateMachine.SwitchState<GameOverState>();
        }
        
        private void PlayerController_MenuPerformed()
        {
            _stateMachine.SwitchState<PauseState>();
        }
    }
}